package com.hziee.mail.dao;

import com.hziee.mail.model.OrderInfoEntity;
import com.hziee.mail.model.SkuInfo;
import com.hziee.mail.model.SkuInfoEntity;
import io.lettuce.core.dynamic.annotation.Param;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author ZX
 */
public interface SkuInfoRepository extends JpaRepository<SkuInfo, Integer>, CrudRepository<SkuInfo, Integer> {

    /**
     * 减少库存数量
     *
     * @param goodsId
     */
    @Transactional
    @Modifying(clearAutomatically = true)
    @Query(nativeQuery = true, value = "UPDATE sku_info SET sku_info.amount = sku_info.amount - :value WHERE sku_info.product_class_id = :goodsId")
    void reduceAmount(@Param("value") Integer value, @Param("goodsId") String goodsId);

    /**
     * 查询库存
     *
     * @param productClassId
     * @return
     */
    SkuInfo findAllByProductClassId(String productClassId);
}
